package com.ruoyi.web.controller.weiriya;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.weiriya.domain.WArea;
import com.ruoyi.weiriya.domain.dto.StatisticalReportDto;
import com.ruoyi.weiriya.domain.dto.StatisticalReportOrderDto;
import com.ruoyi.weiriya.domain.dto.StatisticalReportRecordDto;
import com.ruoyi.weiriya.domain.vo.StatisticalReportVo;
import com.ruoyi.weiriya.service.IWStatisticalReportService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 统计报表Controller
 * 
 * @author lynn
 * @date 2023-08-01
 */
@Api(tags = "统计报表")
@RestController
@RequestMapping("/weiriya/statistical/report")
public class WStatisticalReportController extends BaseController
{
    @Autowired
    private IWStatisticalReportService wStatisticalReportService;

    /**
     * 用户数量统计
     */
    @ApiOperation("用户数量统计")
    @ApiImplicitParams({
            @ApiImplicitParam(name="type",value = "", paramType = "path", allowableValues = "vehicleType,area")
    })
    @GetMapping("/memberNum/{type}")
    public AjaxResult statisticalMemberNum(@PathVariable("type") String type)
    {
        List<StatisticalReportVo> vos = wStatisticalReportService.statisticalMemberNum(type);
        return success(vos);
    }
    /**
     * 平台收入统计
     */
    @ApiOperation("平台收入统计")
    @PostMapping("/income")
    public AjaxResult statisticalIncome(@RequestBody StatisticalReportDto dto)
    {
        List<StatisticalReportVo> vos = wStatisticalReportService.statisticalIncome(dto);
        return success(vos);
    }

    /**
     * 平台订单数量统计
     */
    @ApiOperation("平台订单数量统计")
    @PostMapping("/order")
    public AjaxResult statisticalOrder(@RequestBody StatisticalReportOrderDto dto)
    {
        Map<String, Object> map = new HashMap<>();
        dto.setOrderType("1");
        List<StatisticalReportVo> vipVos = wStatisticalReportService.statisticalOrder(dto);
        map.put("keys", vipVos.stream().map(StatisticalReportVo::getKey).collect(Collectors.toList()));
        map.put("vipOrder", vipVos.stream().map(StatisticalReportVo::getValue).collect(Collectors.toList()));
        dto.setOrderType("2");
        List<StatisticalReportVo> tempVos = wStatisticalReportService.statisticalOrder(dto);
        map.put("tempOrder", tempVos.stream().map(StatisticalReportVo::getValue).collect(Collectors.toList()));
        dto.setOrderType(null);
        List<StatisticalReportVo> allVos = wStatisticalReportService.statisticalOrder(dto);
        map.put("allOrder", allVos.stream().map(StatisticalReportVo::getValue).collect(Collectors.toList()));
        return success(map);
    }

    /**
     * 平台停车或充电记录数量统计
     */
    @ApiOperation("平台停车或充电记录数量统计")
    @PostMapping("/record")
    public AjaxResult statisticalRecord(@RequestBody StatisticalReportRecordDto dto)
    {
        Map<String, Object> map = wStatisticalReportService.statisticalRecord(dto);
        return success(map);
    }

    /**
     * 平台消费记录统计
     */
    @ApiOperation("平台消费记录统计")
    @PostMapping("/consume")
    public AjaxResult statisticalConsume(@RequestBody StatisticalReportDto dto)
    {
        List<StatisticalReportVo> vos = wStatisticalReportService.statisticalConsume(dto);
        return success(vos);
    }

    /**
     * 导出报表
     */
    @ApiOperation("导出报表")
    @Log(title = "导出报表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WArea wArea)
    {
        List<WArea> list = new ArrayList<>();
        ExcelUtil<WArea> util = new ExcelUtil<WArea>(WArea.class);
        util.exportExcel(response, list, "小区信息（停车点位）数据");
    }

}
